const connection = require('../config/database');

const bookController = {
  getAllBooks: async (req, res) => {
    connection.query('SELECT * FROM books', (err, results) => {
      if (err) {
        res.status(500).json({ error: err.message });
        return;
      }
      res.json(results);
    });
  },

  addBook: async (req, res) => {
    const book = req.body;
    connection.query('INSERT INTO books SET ?', book, (err, result) => {
      if (err) {
        res.status(500).json({ error: err.message });
        return;
      }
      res.json('Book added successfully');
    });
  },

  getBookByISBN: async (req, res) => {
    connection.query('SELECT * FROM books WHERE isbn = ?', [req.params.isbn], (err, results) => {
      if (err) {
        res.status(500).json({ error: err.message });
        return;
      }
      res.json(results[0]);
    });
  },

  updateBook: async (req, res) => {
    const book = req.body;
    connection.query('UPDATE books SET ? WHERE id = ?', [book, req.params.id], (err, result) => {
      if (err) {
        res.status(500).json({ error: err.message });
        return;
      }
      res.json('Book updated successfully');
    });
  }
};

module.exports = bookController; 